/**
 * Copyright (c) 时间, Tal.Ben 袁东昊 (ydonghao2@gmail.com).
 *
 * 版权
 */
package com.talben.soudian.fragment;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.kymjs.kjframe.KJDB;

import android.view.View;
import android.widget.AdapterView;

import com.talben.soudian.AppContext;
import com.talben.soudian.TDevice;
import com.talben.soudian.adapter.ApkAdapter;
import com.talben.soudian.adapter.ApkInstallerAdapter;
import com.talben.soudian.adapter.ListBaseAdapter;
import com.talben.soudian.api.SouDianAPI;
import com.talben.soudian.base.BaseListFragment;
import com.talben.soudian.bean.ApkDetail;
import com.talben.soudian.bean.ApkList;
import com.talben.soudian.ui.EmptyLayout;
import com.talben.soudian.ui.OnTabReselectListener;

/**
 * @Title: ApkInstalledFragment.java
 * @Package com.talben.soudian.fragment
 * @Description: TODO(添加描述)
 * @author Tal.Ben
 * @date 2015年6月1日 下午5:41:12
 * @version v1.0
 */
public class ApkInstalledFragment extends BaseListFragment<ApkDetail> implements
		OnTabReselectListener{
	private static final String CACHE_KEY_PREFIX = "apkinstalledlist_";

	@Override
	public void onTabReselect() {
		onRefresh();
	}

	@Override
	protected ApkInstallerAdapter getListAdapter() {
		return new ApkInstallerAdapter(getActivity());
	}
	
	@Override
    protected String getCacheKeyPrefix() {
        return CACHE_KEY_PREFIX + mCatalog;
    }
	
	@Override
    protected ApkList parseList(JSONArray JArray) throws Exception {
		ApkList list = null;
        try {
            list = new ApkList();
            KJDB db = KJDB.create(getActivity());
    		List<ApkDetail> all = db.findAll(ApkDetail.class);
    		for (ApkDetail apkDetail : all) {
    			if (TDevice.checkApkExist(getActivity(), apkDetail.getPackageName())) {
    				apkDetail.setBtnStatus("打开");
    			}
    			list.getList().add(apkDetail);
			}
        } catch (NullPointerException e) {
            list = new ApkList();
        }
        return list;
    }
	
	@Override
    protected ApkList readList(Serializable seri) {
        return ((ApkList) seri);
    }
	
	@Override
    protected void sendRequestData() {
        SouDianAPI.getApkList(mCatalog, mCurrentPage, 1 ,mHandler);
    }
	
	@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        ApkDetail apkDetail = mAdapter.getItem(position);
        if (apkDetail != null) {
//            UIHelper.showNewsRedirect(view.getContext(), news);
            // 放入已读列表
//            saveToReadedList(view, NewsList.PREF_READED_NEWS_LIST, news.getId()
//                    + "");
        }
    }
	
	@Override
    protected void executeOnLoadDataSuccess(List<ApkDetail> data) {
        if (mCatalog == ApkList.CATALOG_ALL
                || mCatalog == ApkList.CATALOG_RECOMMEND) {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            if (mState == STATE_REFRESH)
                mAdapter.clear();
            mAdapter.addData(data);
            mState = STATE_NOMORE;
            mAdapter.setState(ListBaseAdapter.STATE_NO_MORE);
            return;
        }
        super.executeOnLoadDataSuccess(data);
    }
	
	@Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }
	
	@Override
    protected long getAutoRefreshTime() {
        // 最新apk两小时刷新一次
        if (mCatalog == ApkList.CATALOG_ALL) {
            return 2 * 60 * 60;
        }
        return super.getAutoRefreshTime();
    }
}

